package com.web_bloks.widget.client;

import com.web_bloks.system.client.Logger;
import com.web_bloks.types.client.HashCollections;

/**
 * Holds a list of widgets
 * 
 * @author Jean-Lou Dupont
 *
 */
public class WidgetCollection {

	final static String thisClass = "com.web_bloks.widget.WidgetCollection";
	
	static WidgetCollection self = new WidgetCollection();
	
	protected WidgetCollection() {
		super();
	}
	
	public static WidgetCollection create(String collectionName) {
		//Logger.logInfo(thisClass+".create, collectionName["+collectionName+"]");
	
		HashCollections.create(collectionName);
		
		return self;
	}
	
	
	public WidgetEx getWidget(String collectionName, String key){
		Logger.logInfo(thisClass+".getWidget, key["+key+"]");
		if ( null == key )
			return null;
		if ( key.length() == 0 )
			return null;
		HashCollections c = HashCollections.create(collectionName);
		return (WidgetEx) c.get(key);
	}
	
	public void addWidget(String collectionName, String key, WidgetEx w) {
		Logger.logInfo(thisClass+".addWidget, collectionName["+collectionName+"] key["+key+"]");
		if ( null == key )
			throw new RuntimeException(thisClass+".addWidget: key can't be null");
		if ( key.length() == 0 )
			throw new RuntimeException(thisClass+".addWidget: key can't be empty");
		HashCollections c = HashCollections.create(collectionName);
		c.add(key,w);
	}
	
	public void removeWidget(String collectionName, String key) {
		Logger.logInfo(thisClass+".removeWidget, key["+key+"]");
		if ( null == key )
			throw new RuntimeException(thisClass+".removeWidget: key can't be null");
		if ( key.length() == 0 )
			throw new RuntimeException(thisClass+".removeWidget: key can't be empty");
		HashCollections c = HashCollections.create(collectionName);
		c.remove(key);
	}
	
	public void clearWidgets(String collectionName) {
		Logger.logInfo(thisClass+".clearWidgets");
		HashCollections c = HashCollections.create(collectionName);
		c.clear();
	}
	
}//END